package keeto.com.vn.bms.ui.activity;

import java.util.ArrayList;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.model.InfoMessegerModel;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.adapter.InfoChatAdapter;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import keeto.com.vn.bms.utils.Utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class InfoChatActivity extends Activity implements OnClickListener, OnRefreshListener<ListView>{
	private final static String EXTRA_MODEL_MSG = "MessegerModel";
	private final int PAGE_LIMIT = 30;
	private int PageNumber = 1;
	private ImageButton btnBack;
	private TextView tvTittle;
	private PullToRefreshListView listViewChatMsg;
	private EditText edtMsgChat;
	private Button btnSend;
	private InfoMessegerModel messegerModel;
	private InfoChatAdapter mAdapter;
	private ArrayList<InfoMessegerModel> mArrayList = new ArrayList<InfoMessegerModel>();;
	private DetailMsgListener listener = new DetailMsgListener();
	private BMSSharedPreferences preferences;
	private ProgressBar mBar;
	private static String CurrenmsgId;

	public static void  actionlunch(Context context, InfoMessegerModel model) {
		Intent intent = new Intent(context, InfoChatActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(EXTRA_MODEL_MSG, model);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info_chat_layout);
		Intent i = getIntent();
		Bundle extras = i.getExtras();
		messegerModel = (InfoMessegerModel) extras.get(EXTRA_MODEL_MSG);
		preferences = new BMSSharedPreferences(InfoChatActivity.this);
		initUIComponent();
	}

	private void initUIComponent(){
		btnBack = (ImageButton)findViewById(R.id.btn_tittle_back);
		tvTittle = (TextView)findViewById(R.id.tv_title_name);
		listViewChatMsg = (PullToRefreshListView)findViewById(R.id.listViewChatmsg);
		edtMsgChat = (EditText)findViewById(R.id.edtChatmsg);
		btnSend = (Button)findViewById(R.id.btn_Send_msg);
		mBar = (ProgressBar)findViewById(R.id.pBar_detail_chat);

		btnBack.setOnClickListener(this);
		tvTittle.setText(messegerModel.getMsgtittle());
		btnSend.setOnClickListener(this);
		mAdapter = new InfoChatAdapter(InfoChatActivity.this, mArrayList);
		listViewChatMsg.setAdapter(mAdapter);
		listViewChatMsg.setOnRefreshListener(this);
		listViewChatMsg.setMode(Mode.BOTH);
		//		Display display = getWindowManager().getDefaultDisplay();
		//		Point size = new Point();
		//		display.getSize(size);
		//		int width = size.x;
		//		int height = size.y;
		//		listViewChatMsg.getRefreshableView().scrollTo(0, height);
		mBar.setVisibility(View.GONE);
		CurrenmsgId = messegerModel.getMsgID();
		BMSCore.getInstance(this).taskGetDetailMesseger(CurrenmsgId, preferences.getUserAuthKey(InfoChatActivity.this),PAGE_LIMIT,PageNumber, listener);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_tittle_back:
			InfoChatActivity.this.finish();

			break;
		case R.id.btn_Send_msg:
			SendComment();
			break;
		default:
			break;
		}
	}

	private void SendComment(){
		String contentmsg = "";
		contentmsg =edtMsgChat.getText().toString();
		mBar.setVisibility(View.VISIBLE);
		if(!Utils.validateValue(contentmsg).equals("")){

			BMSCore.getInstance(InfoChatActivity.this).taskSendMesseger(messegerModel.getUserID(),"", contentmsg, preferences.getUserLoginFullName(InfoChatActivity.this), preferences.getUserAuthKey(this), listener);

		}
	}

	class DetailMsgListener extends BMSListener{

		@Override
		public void finishGetDetailMesseger(final int returnCode, final int pageCount,
				final ArrayList<InfoMessegerModel> ListDetailMsg) {
			// TODO Auto-generated method stub
			super.finishGetDetailMesseger(returnCode, pageCount, ListDetailMsg);
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK && ListDetailMsg != null){						

						if (PageNumber < pageCount){
							PageNumber +=1;
						}else{
							PageNumber =-1;
						}
						for (int i = ListDetailMsg.size()-1 ; i >= 0; i--){
							mArrayList.add(ListDetailMsg.get(i));
						}
						mAdapter.notifyDataSetChanged();
					}else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(InfoChatActivity.this, InfoChatActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(InfoChatActivity.this,InfoChatActivity.this.getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					}else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(InfoChatActivity.this, InfoChatActivity.this.getResources().getString(R.string.error_perpose), Toast.LENGTH_SHORT).show();
					}
					mBar.setVisibility(View.GONE);
					listViewChatMsg.onRefreshComplete();
				}
			});
		}

		@Override
		public void finishSendMesseger(final int returnCode, String resultMsg,
				final String msgId) {
			// TODO Auto-generated method stub
			super.finishSendMesseger(returnCode, resultMsg, msgId);
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					if (returnCode == IBMSProtocol.STATUS_CODE_OK && msgId != null){
						PageNumber=1;
						edtMsgChat.setText("");
						BMSCore.getInstance(InfoChatActivity.this).taskGetDetailMesseger(CurrenmsgId, preferences.getUserAuthKey(InfoChatActivity.this),PAGE_LIMIT,PageNumber, listener);
					}else if (returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(InfoChatActivity.this, InfoChatActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
						edtMsgChat.setText("");
					} else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						edtMsgChat.setText("");
						Toast.makeText(InfoChatActivity.this, InfoChatActivity.this.getResources().getString(R.string.error_perpose), Toast.LENGTH_SHORT).show();
					}
					mBar.setVisibility(View.GONE);
				}
			});
		}

	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		mBar.setVisibility(View.VISIBLE);
		if (PageNumber >0){
			BMSCore.getInstance(InfoChatActivity.this).taskGetDetailMesseger(CurrenmsgId, preferences.getUserAuthKey(InfoChatActivity.this),PAGE_LIMIT,PageNumber, listener);
		
		}else{
			new Handler().post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
				listViewChatMsg.onRefreshComplete();	
				}
			});
		}
	}

}
